package com.kingyea.mobilepolice.apimanage.provider.controller;

import com.kingyea.mobilepolice.apimanage.log.OperateType;
import com.kingyea.mobilepolice.apimanage.log.OperationLogAnnotation;
import com.kingyea.mobilepolice.apimanage.provider.bean.Provider;
import com.kingyea.mobilepolice.apimanage.provider.service.ProviderService;
import com.kingyea.mobilepolice.apimanage.provider.vo.ProviderVO;
import com.kingyea.mobilepolice.system.Result;
import com.kingyea.mobilepolice.user.bean.User;
import com.kingyea.mobilepolice.user.client.UserServiceClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description Provider 控制层
 * @date Created in 2018-02-27 10:56:01
 */
@Api(value = "服务方管理接口 ", description = " 服务方管理相关 ")
@RequestMapping(value = "/provider")
@RestController
public class ProviderController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProviderController.class);

    @Autowired
    private ProviderService providerService;

    @Autowired
    private UserServiceClient userServiceClient;


    /**
     * @param map
     * @return com.alibaba.fastjson.JSONObject
     * @author Mr.Lin
     * @date 2018/3/2 15:36
     */
    @ApiOperation(value = "根据搜索条件搜索服务方列表", notes = "", response = ProviderVO.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "Date")
            , @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "Date")
            , @ApiImplicitParam(name = "pageNum", value = "第几页", required = true, dataType = "int")
            , @ApiImplicitParam(name = "pageSize", value = "每页数据数量大小", required = true, dataType = "int")
            , @ApiImplicitParam(name = "keyword", value = " 关键字keyword，订单号和代理商名字 ", required = false, dataType = "Integer")
            , @ApiImplicitParam(name = "searchType", value = " 搜索类型（provider.提供商名字 serialNo.接口标识 contacts.联系人 contactsTel.联系人号码 all.所有） ", required = true, dataType = "String")
    })
    @OperationLogAnnotation(operateName = "带参数查询服务方列表", operateType = OperateType.SELECT, module = "provider")
    @RequestMapping(value = "/searchByCondition", method = RequestMethod.POST)
    public Object searchByCondition(@RequestBody Map<String, Object> map) throws Exception {
        if (map.containsKey("roleId")) {
            if (!"".equals(map.get("roleId")) && Integer.parseInt(map.get("roleId").toString()) == 0) {
                map.put("idAdmin", null);
            } else {
                map.put("isAdmin", userServiceClient.getCurrentUserInfo().getData().getUserId());
            }
        }
        if (map.containsKey("status")) {
            if (!"".equals(map.get("status")) && Integer.parseInt(map.get("status").toString()) == 0) {
                map.put("status", 1);
                map.put("_status", 2);
            }
        }
        Object data = providerService.searchByCondition(map);
        return data;
    }


    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @OperationLogAnnotation(operateName = "查询全部服务方列表", operateType = OperateType.SELECT, module = "provider")
    @RequestMapping(method = RequestMethod.GET)
    public Object list() throws Exception {
        return providerService.list(null);
    }

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @OperationLogAnnotation(operateName = "通过ID查找服务方", operateType = OperateType.SELECT, module = "provider")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Object detail(@PathVariable Integer id) throws Exception {
        return providerService.getById(id);
    }

    /**
     * 创建
     *
     * @param provider
     * @return
     */
    @OperationLogAnnotation(operateName = "创建服务方", operateType = OperateType.INSERT, module = "provider")
    @RequestMapping(method = RequestMethod.POST)
    public Object create(@RequestBody Provider provider) throws Exception {
        Result<User> result = userServiceClient.getCurrentUserInfo();
        provider.setCreateById(result.getData().getUserId());
        provider.setCreateByName(result.getData().getName());
        return providerService.save(provider);
    }

    /**
     * 更新
     *
     * @param provider
     * @return
     */
    @ApiOperation(value = "更新", notes = "", response = Integer.class)
    @OperationLogAnnotation(operateName = "更新服务方", operateType = OperateType.UPDATE, module = "provider")
    @RequestMapping(method = RequestMethod.PUT)
    public Object update(@RequestBody Provider provider) throws Exception {
        Result<User> result = userServiceClient.getCurrentUserInfo();
        provider.setModifiedById(result.getData().getUserId());
        return providerService.update(provider);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除", notes = "", response = Integer.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int")
    })
    @OperationLogAnnotation(operateName = "删除服务方", operateType = OperateType.DELETE, module = "provider")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Object changeDelFlagById(@PathVariable Integer id) throws Exception {
        return providerService.deleteById(id);
    }

    /**
     * 带条件统计服务方数量
     *
     * @param map
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "带条件统计服务方数量", notes = "", response = ProviderVO.class)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "startTime", value = "注册开始时间", required = true, dataType = "Date")
            , @ApiImplicitParam(name = "endTime", value = "注册结束时间", required = true, dataType = "Date")

    })
    @OperationLogAnnotation(operateName = "带条件统计服务方数量", operateType = OperateType.SELECT, module = "requester")
    @RequestMapping(value = "/countByCondition", method = RequestMethod.POST)
    public Object countByCondition(@RequestBody Map<String, Object> map) throws Exception {
        Object data = providerService.countByCondition(map);
        return data;
    }

}
